import { poiClickCallback } from '@/service';
import { generateListLayerName } from '@/service/layer';
import { useSceneStore } from '@/store/scene';
import { retFilePath } from '@/utils';
import { nextTick } from 'vue';

export const addPoiEffect = (selEffect: any) => {
  const sceneStore = useSceneStore();

  // poi特效添加
  function addPoi() {
    const { cimInstance } = window;
    if (!selEffect.value) return;
    const effectTemporaryAttr: any = sceneStore.effectTemporaryAttr;
    console.log('临时属性值-----', effectTemporaryAttr);
    const imageConfig = {
      imageUrl: effectTemporaryAttr
        ? effectTemporaryAttr.imageUrl
        : retFilePath(selEffect.value.quoteUrl),
      imageHorizontalOrigin: 'CENTER',
      imageVerticalOrigin: 'BOTTOM',
      imageOffset: [0, 0],
      imageScale: effectTemporaryAttr ? effectTemporaryAttr.imageScale : 1,
      imageWidth: effectTemporaryAttr ? effectTemporaryAttr.imageWidth : 50,
      imageHeight: effectTemporaryAttr ? effectTemporaryAttr.imageHeight : 50,
      imageHeightReference: effectTemporaryAttr
        ? effectTemporaryAttr.imageHeightReference
        : 'CLAMP_TO_GROUND',
      imageIsBlink: effectTemporaryAttr ? effectTemporaryAttr.imageIsBlink : true,
      imageScaleByDistanceArray: effectTemporaryAttr
        ? effectTemporaryAttr.imageScaleByDistanceArray
        : [1, 0.5, 500, 2],
      imageMinDisplayDistance: effectTemporaryAttr
        ? effectTemporaryAttr.imageMinDisplayDistance
        : 0,
      imageMaxDisplayDistance: effectTemporaryAttr
        ? effectTemporaryAttr.imageMaxDisplayDistance
        : 10000000000000000,
    };
    const infoConfig = {
      infoValue: 'POI点标签文本',
      infoFont: effectTemporaryAttr ? effectTemporaryAttr.infoFont : '10px 微软雅黑',
      infoFillColor: effectTemporaryAttr ? effectTemporaryAttr.infoFillColor : '#ffffff',
      infoHorizontalOrigin: 'CENTER',
      infoVerticalOrigin: 'BOTTOM',
      infoOffset: effectTemporaryAttr ? effectTemporaryAttr.infoOffset : [0, -50, 0],
      infoVertical: effectTemporaryAttr ? effectTemporaryAttr.infoVertical : false,
      infoHeightReference: effectTemporaryAttr
        ? effectTemporaryAttr.infoHeightReference
        : 'CLAMP_TO_GROUND',
      infoScaleByDistanceArray: effectTemporaryAttr
        ? effectTemporaryAttr.infoScaleByDistanceArray
        : [1, 0.5, 500, 2],
      infoMinDisplayDistance: effectTemporaryAttr ? effectTemporaryAttr.infoMinDisplayDistance : 0,
      infoMaxDisplayDistance: effectTemporaryAttr
        ? effectTemporaryAttr.infoMaxDisplayDistance
        : 10000000000000000,
    };
    const flagConfig = {
      flagValue: 'flag',
      flagFont: '25px 微软雅黑',
      flagFillColor: '#FFFF00',
      flagHorizontalOrigin: 'CENTER',
      flagVerticalOrigin: 'BOTTOM',
      flagOffset: [0, -45],
    };
    const modelJson = {
      enable: true,
      name:
        (selEffect.value.effectType === 1 ? 'poi' : '图片') +
        Math.random().toString().split('.')[1],
      type: 'drawlabel',

      drawCall: function (e: any) {
        const properties = e.properties;
        properties.name = 'poi_' + properties.id;
        properties.coordinates = e.coordinates;
        properties.layerName = modelJson.name;
        properties.defaultShow = true; // 默认常显
        Object.assign(properties, imageConfig);
        Object.assign(properties, infoConfig);
        // 一个点生成一个图层
        cimInstance.api.endDrawLabel();
        setTimeout(() => {
          poiClickCallback({
            code: 'effect',
            clickObject: {
              id: '0_image',
              name: 'poi_0_image',
              layerName: properties.layerName,
            },
          });
        }, 300);
        // 一定要加延迟
        setTimeout(() => {
          if (
            sceneStore.effectTemporaryAttr &&
            sceneStore.poiEffectTemporaryLayernames.length &&
            sceneStore.poiEffectTemporaryLayernames[0] !== ''
          ) {
            updateSkipPointAttr(
              sceneStore.poiEffectTemporaryLayernames[
                sceneStore.poiEffectTemporaryLayernames.length -
                  (sceneStore.poiEffectTemporaryLayernames[0] === 'test' ? 3 : 2)
              ]
            );
            sceneStore.poiEffectTemporaryLayernames[0] = '';
          }
        }, 0);
        addPoi();
        return properties;
      },
    };
    const layerParam: any = {
      code: Math.random().toString().split('.')[1],
      show: true,
      json: modelJson,
      name: generateListLayerName('drawlabel'),
      layerName: modelJson.name,
      desc: '',
      type: modelJson.type,
    };
    cimInstance.api.closeSceneOffset();
    sceneStore.layerList.unshift(layerParam);
    sceneStore.poiEffectTemporaryLayernames.push(layerParam.layerName);
  }

  // 更新跳过的点位的属性
  function updateSkipPointAttr(layerName: string) {
    const { cimInstance, Cesium } = window;
    const effectTemporaryAttr: any = sceneStore.effectTemporaryAttr;
    const result = cimInstance.api.getGraphicInfo(layerName, 'poi_0_image', {
      properties: true,
    });
    const properties = result.properties.getValue(new Cesium.JulianDate());
    let imageParam: any = {
      imageUrl: effectTemporaryAttr.imageUrl,
      imageWidth: Number(effectTemporaryAttr.imageWidth),
      imageHeight: Number(effectTemporaryAttr.imageHeight),
      imageScale: effectTemporaryAttr.imageScale, // 标签闪烁
      imageIsBlink: effectTemporaryAttr.imageIsBlink, // 标签闪烁
      imageHeightReference: effectTemporaryAttr.imageHeightReference, // 标签贴地
      imageScaleByDistanceArray: effectTemporaryAttr.imageScaleByDistanceArray.map((v: any) =>
        Number(v)
      ),
    };
    let infoParam: any = {
      infoValue: effectTemporaryAttr.infoValue,
      infoFillColor: effectTemporaryAttr.infoFillColor,
      infoFont: effectTemporaryAttr.infoFont + 'px 微软雅黑',
      infoOffset: effectTemporaryAttr.infoOffset.map((v: any) => Number(v)),
      infoVertical: effectTemporaryAttr.infoVertical,
      infoHeightReference: effectTemporaryAttr.infoHeightReference, // 标签贴地
      infoScaleByDistanceArray: effectTemporaryAttr.infoScaleByDistanceArray.map((v: any) =>
        Number(v)
      ),
    };
    if (effectTemporaryAttr.defaultShow) {
      imageParam = {
        ...imageParam,
        imageMinDisplayDistance: 0,
        imageMaxDisplayDistance: 10000000000000000,
      };
      infoParam = {
        ...infoParam,
        infoMinDisplayDistance: 0,
        infoMaxDisplayDistance: 10000000000000000,
      };
    } else {
      imageParam = {
        ...imageParam,
        imageMinDisplayDistance: 0,
        imageMaxDisplayDistance: 10000,
      };
      infoParam = {
        ...infoParam,
        infoMinDisplayDistance: 0,
        infoMaxDisplayDistance: 10000,
      };
    }
    cimInstance.api.updateGraphicById(layerName, '0_image', {
      image: imageParam,
      coordinates: properties.coordinates.map((v: any) => Number(v)),
    });
    cimInstance.api.updateGraphicById(layerName, '0_info', {
      info: infoParam,
      coordinates: properties.coordinates.map((v: any) => Number(v)),
    });
  }

  return {
    addPoi,
  };
};
